function course() // меняем направление вражеских танков
{
    for ( var i = 0; i < enemies.length; i++ )
    {
        if (( !enemies[i].dead ) && ( !enemies[i].pursue_enemies ))
        {
            getRandomTurn( enemies[i] );  
        }
    }
}

function checkPathAndAttack( tankObj, way, turn1, turn2 )
{
    var tankI = Math.round( tank.y / BLOCK_SIDE );
    var tankJ = Math.round( tank.x / BLOCK_SIDE );
    var enI = Math.round( tankObj.y / BLOCK_SIDE );
    var enJ = Math.round( tankObj.x / BLOCK_SIDE );
    var pathClear = true;
    
    if ( way == "hor" )
    {
        var start = ( tankJ >= enJ ) ? enJ : tankJ ;
        var finish = ( tankJ >= enJ ) ? tankJ : enJ ;
        for ( var i = start; i <= finish; i++ )
        {
            if ( level[enI][i].type != 0 )
            {
                pathClear = false;
            }
        }
    }
    else if ( way == "ver" )
    {
        var start = ( tankI >= enI ) ? enI : tankI ;
        var finish = ( tankI >= enI ) ? tankI : enI ;
        for ( var i = start; i <= finish; i++ )
        {
            if ( level[i][enJ].type != 0 )
            {
                pathClear = false;
            }
        }
    }
    if ( pathClear )
    {
        if ( start == (( way == "hor" ) ? enJ : enI ))
        {
            tankObj.turn( turn1 );
        }
        else
        {
            tankObj.turn( turn2 );
        }
        tankObj.launchRocket();
        tankObj.pursue_enemies = true;
        if ( Math.abs( start - finish ) <= 2 )
        {
            tankObj.pursue_enemies = true;
            tankObj.dX = 0;
            tankObj.dY = 0;
        }
    }
    
    return pathClear;
}

function simpleIntelligence( tankObj )
{
    if ( tankObj.intelligence == "type1" ) 
    {
        if (( tank.y + tank.height/2 < tankObj.y + tankObj.height) &&
            ( tank.y + tank.height > tankObj.y + tankObj.height/2 ))
        { // на одной горизонтали
            return checkPathAndAttack( tankObj, "hor", "right", "left" );
        }
        if (( tank.x + tank.width/2 < tankObj.x + tankObj.width ) &&
            ( tank.x + tank.width > tankObj.x + tankObj.width/2 ))
        { // на одной вертикали
            return checkPathAndAttack( tankObj, "ver", "down", "up" );
        }
    }
}

function orientation_x( tankObj )
{
    if ( tank.x + tank.width < tankObj.x )
    {
        tankObj.turn( "left" );
    }
    if ( tank.x > tankObj.x + tankObj.width )
    {
        tankObj.turn( "right" );
    }
}

function orientation_y( tankObj )
{ 
    if ( tank.y + tank.height < tankObj.y )
    {
        tankObj.turn( "up" );
    }
    if ( tank.y > tankObj.y + tankObj.height )
    {
        tankObj.turn( "down" );
    }
}

function orientation( tankObj )
{ alert('1');
    if (( tank.y < tankObj.y + tankObj.height) && 
        ( tank.y + tank.height > tankObj.y ))
    {
        orientation_x( tankObj );
    }
    else
    {
        if (( tank.x < tankObj.x + tankObj.width ) && 
            ( tank.x + tank.width > tankObj.x ))
        {
            orientation_y( tankObj );
        }
        else
        {
            if ( tank.x < tankObj.x )
            {
                tankObj.turn("left");
            }
            if ( tank.x > tankObj.x + tankObj.width )
            {
                tankObj.turn("right");
            }
        }
    }
}

function pursuit( tankObj )
{
    if ( tankObj.intelligence == "type2" )
    {
       /* if (( tank.y + tank.height/2 < tankObj.y + tankObj.height) &&
                    ( tank.y + tank.height > tankObj.y + tankObj.height/2 ))
                { // на одной горизонтали
                    checkPathAndAttack( tankObj, "hor", "right", "left" );
                } else
                if (( tank.x + tank.width/2 < tankObj.x + tankObj.width ) &&
                    ( tank.x + tank.width > tankObj.x + tankObj.width/2 ))
                { // на одной вертикали
                    checkPathAndAttack( tankObj, "ver", "down", "up" );
                }*/
                
        if (( tank.x + tank.width > tankObj.x - VISIBILITY ) &&
            ( tank.x < tankObj.x + tankObj.width + VISIBILITY ) &&
            ( tank.y + tank.height > tankObj.y - VISIBILITY ) &&
            ( tank.y < tankObj.y + tankObj.height + VISIBILITY ))
        { // танк игрока попал в область преследования
            tankObj.pursue_enemies = true;
            //enemiesCourseTimer--;
            
            
            if ( tankObj.attack )
            {
                if ( Math.abs( tank.x - tankObj.x ) <= Math.abs( tank.y - tankObj.y ))
                {
                    if ( tankObj.x < tank.x )
                    {
                        tankObj.turn( "right" );
                    }
                    else if ( tankObj.x > tank.x )
                    {
                        tankObj.turn( "left" );
                    }
                }
                else
                {
                    if ( tankObj.y < tank.y )
                    {
                        tankObj.turn( "down" );
                    }
                    else if ( tankObj.y > tank.y )
                    {
                        tankObj.turn( "up" );
                    }
                }
            }
            if ((( tank.x + tank.width/2 < tankObj.x + tankObj.width ) &&
                 ( tank.x + tank.width > tankObj.x + tankObj.width/2 )) ||
                (( tank.y + tank.height/2 < tankObj.y + tankObj.height ) &&
                 ( tank.y + tank.height > tankObj.y + tankObj.height/2 )))
            {
                tankObj.dX = 0;
                tankObj.dY = 0;
                if ( !tankObj.attack )
                {
                    tankObj.attack = true;
                   /* if (( tank.y + tank.height/2 < tankObj.y + tankObj.height) &&
                        ( tank.y + tank.height > tankObj.y + tankObj.height/2 ))
                    { // на одной горизонтали
                        checkPathAndAttack( tankObj, "hor", "right", "left" );
                    }
                    if (( tank.x + tank.width/2 < tankObj.x + tankObj.width ) &&
                        ( tank.x + tank.width > tankObj.x + tankObj.width/2 ))
                    { // на одной вертикали
                        checkPathAndAttack( tankObj, "ver", "down", "up" );
                    }*/
                }
                else
                {
                   // tankObj.launchRocket();
                }
                //checkPathAndAttack( tankObj, "hor", "right", "left" );
                //checkPathAndAttack( tankObj, "ver", "down", "up" );
            }
        }
        else
        if (( tank.y + tank.height/2 < tankObj.y + tankObj.height) &&
            ( tank.y + tank.height > tankObj.y + tankObj.height/2 ))
        { // на одной горизонтали
            checkPathAndAttack( tankObj, "hor", "right", "left" );
        }
        else
        if (( tank.x + tank.width/2 < tankObj.x + tankObj.width ) &&
            ( tank.x + tank.width > tankObj.x + tankObj.width/2 ))
        { // на одной вертикали
            checkPathAndAttack( tankObj, "ver", "down", "up" );
        }
        else
        {
            tankObj.pursue_enemies = false;
            if (( tankObj.dX == 0 ) && ( tankObj.dY == 0 ))
            {
                getRandomTurn( tankObj );
            }
        }
    }
}